package cn.droidlover.xdroidmvp.view;

import android.content.Context;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import cn.droidlover.xdroidmvp.kit.Kits;

public class MarginDecoration extends RecyclerView.ItemDecoration {
    private int margin;
    private int count;

    public MarginDecoration(Context context) {
        margin = Kits.Dimens.dpToPxInt(context, 10);
    }

    public MarginDecoration(Context context, int count) {
        margin = Kits.Dimens.dpToPxInt(context, 10);
        this.count = count;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {//由于每行都只有2个，所以第一个都是2的倍数，把左边距设为0   
        int index = parent.getChildLayoutPosition(view);
        if (count != 0) {
            if (index % count == 0) {
                outRect.set(margin, 0, 0, margin);
            } else if (index % count == count - 1) {
                outRect.set(margin, 0, margin, margin);
            } else {
                outRect.set(margin, 0, 0, margin);
            }
        } else {
            outRect.set(margin / 2, margin / 2, margin / 2, margin / 2);

        }

    }
}

